package com.wyw.hemerocallis.stream.wyw;

import com.alibaba.fastjson.JSON;
import com.wyw.hemerocallis.vo.WywMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;

/**
 * 使用自定义的通信信道WywSource实现对消息的发送
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
@Slf4j
@EnableBinding(WywSource.class)
public class WywSendService {

    private final WywSource wywSource;

    public WywSendService(WywSource wywSource) {
        this.wywSource = wywSource;
    }

    /**
     * 使用自定义的输出信道发送消息
     */
    public void sendMessage(WywMessage message) {
        String _message = JSON.toJSONString(message);
        log.info("in WywSendService send message: [{}]", _message);
        wywSource.wywOutput().send(MessageBuilder.withPayload(_message).build());
    }

}
